﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace EFTest
{
    public class EFTestContext : DbContext
    {
        public EFTestContext()
            : base("EFTestConntionString")
        {
        }
        public DbSet<User> User { get; set; }

        public DbSet<Order> Order { get; set; }

        public DbSet<Order_2> Order_2 { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           
            //modelBuilder.Entity<Order>()
            //   .HasKey(p => p.Id).Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<Order_2>()
                //.ToTable("OrderExt")
                .HasKey(p => p.Id).Property(p => p.Id).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            //modelBuilder.Entity<Order_2>()
            //   .Property(p => p.RowV).IsConcurrencyToken();

            //modelBuilder.Entity<Order>()
            //    .HasOptional(p => p.Order2)
            //    .WithRequired();

            //modelBuilder.Entity<Order_2>()
            //    .ToTable("Order2")
            //    .HasKey(p => p.Id);
            //modelBuilder.Entity<Order>()
            //    .HasRequired(p => p.Buyer).WithOptional().Map(p =>
            //    {
            //        p.MapKey("BuyerId");
            //    });

            //modelBuilder.Entity<Order>()
            //    .HasRequired(p => p.Seller).WithOptional().Map(p =>
            //    {
            //        p.MapKey("SellerId");
            //    });
            //modelBuilder.Entity<Order>()
            // .HasRequired(p => p.Seller).WithMany();


            //WithOptional order 外键
            //WithRequiredDependent 1：1，order外键



            base.OnModelCreating(modelBuilder);
        }
    }

    public class User
    {
        public Guid Id { get; set; }

        public string LoginId { get; set; }

        public string pwd { get; set; }
    }

    public class Order
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        //public Guid SellerId { get; set; }
        //public Guid BuyerId { get; set; }

        //public User Seller { get; set; }
        //public User Buyer { get; set; }

        //public virtual Order_2 Order2 { get; set; }
    }

    public class Order_2
    {
        //[Key, Required, DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
        public Guid Id { get; set; }

        //[ConcurrencyCheck]
        public string _2_GGG { get; set; }

        public string RowV { get; set; }
    }

    public class DataBaseInit
    {
        public static void Init()
        {

            Database.SetInitializer<EFTestContext>(new EFTestContextDataBaseInit());
            Database.SetInitializer<EFTestContext>(new EFTestContextDataInit());
        }

        public class EFTestContextDataBaseInit : IDatabaseInitializer<EFTestContext>
        {
            public void InitializeDatabase(EFTestContext context)
            {
                context.Database.CreateIfNotExists();
            }
        }
        public class EFTestContextDataInit : DropCreateDatabaseIfModelChanges<EFTestContext>
        {
            protected override void Seed(EFTestContext context)
            {
                var order = new Order() { Name = "9999999999", Id = new Guid("6C0823A1-60A7-442A-9CE1-7B795F196807") };
                context.Order.Add(order);
                context.SaveChanges();
                context.Order_2.Add(new Order_2() { Id = order.Id, _2_GGG = "11111111111111" });
                base.Seed(context);
            }
        }
    }
}
